using AzrngTools.ViewModels.Encode;
using AzrngTools.ViewModels.Encrypts;
using AzrngTools.ViewModels.Format;
using AzrngTools.ViewModels.Other;
using AzrngTools.ViewModels.Setting;
using AzrngTools.ViewModels.TextHandle;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Collections.ObjectModel;

namespace AzrngTools.ViewModels;

/// <summary>
/// 主界面
/// </summary>
public partial class MainWindowViewModel : ViewModelBase
{
    public MainWindowViewModel()
    {
        CreateMenuBars();
        CurrentPage = new OverviewPageViewModel();
        Motto = "放下个人素质，享受缺德人生";
    }

    /// <summary>
    /// 菜单集合
    /// </summary>
    [ObservableProperty]
    private ObservableCollection<MenuBar> _menuBars;

    /// <summary>
    /// 选择的页面
    /// </summary>
    [ObservableProperty]
    private MenuBar _selectedListItem;

    /// <summary>
    /// 当前页面
    /// </summary>
    [ObservableProperty]
    private ViewModelBase _currentPage;

    /// <summary>
    /// 座右铭
    /// </summary>
    [ObservableProperty]
    private string _motto;

    /// <summary>
    /// 创建导航列表
    /// </summary>
    private void CreateMenuBars()
    {
        MenuBars =
        [
            new MenuBar("首页", typeof(OverviewPageViewModel)),
            new MenuBar
            {
                Title = "生成类型工具",
                Child =
                [
                    new MenuBar("GUID生成器", typeof(GuidShowPageViewModel)),
                    new MenuBar("HASH", typeof(HashPageViewModel)),
                    new MenuBar("HMAC HASH", typeof(HmacHashPageViewModel)),
                    new MenuBar("AES", typeof(AesEncryptPageViewModel)),
                    new MenuBar("DES", typeof(DesEncryptPageViewModel)),
                    new MenuBar("SM4", typeof(Sm4EncryptPageViewModel)),
                    new MenuBar("RSA", typeof(RsaEncryptPageViewModel)),
                    new MenuBar("Md预览", typeof(MarkdownPageViewModel)),
                    new MenuBar("Json Schema生成", typeof(JsonSchemaPageViewModel))
                ]
            },
            new MenuBar
            {
                Title = "编码/解码类型工具",
                Child =
                [
                    new MenuBar("BASE64文本", typeof(Base64EncodePageViewModel)),
                    new MenuBar("JWT解码器", typeof(JwtEncodePageViewModel)),
                    new MenuBar("Gzip编码解码", typeof(GzipEncodePageViewModel)),
                    new MenuBar("编码解码", typeof(EncodePageViewModel)),
                ]
            },
            new MenuBar
            {
                Title = "格式化类型工具",
                Child =
                [
                    new MenuBar("文本压缩", typeof(StringPageViewModel)),
                    new MenuBar("JSON格式化", typeof(JsonPageViewModel)),
                    new MenuBar("SQL操作", typeof(SqlFormatPageViewModel)),
                    new MenuBar("XML转HTML", typeof(XmlToHtmlPageViewModel)),
                    new MenuBar("简单正则解析", typeof(RegexAnalysisViewModel))
                ]
            },
            new MenuBar
            {
                Title = "其他",
                Child =
                [
                    new MenuBar("翻译", typeof(TranslatorPageViewModel)),
                    new MenuBar("今日新闻", typeof(TodayNewsPageViewModel))
                ]
            },
            new MenuBar
            {
                Title = "系统设置",
                Child =
                [
                    new MenuBar("硬件信息", typeof(HardwarePageViewModel)),
                    new MenuBar("关于", typeof(AboutPageViewModel))
                ]
            }
        ];
    }

    /// <summary>
    /// 页面切换
    /// </summary>
    /// <param name="value"></param>
    partial void OnSelectedListItemChanged(MenuBar value)
    {
        if (value?.MenuType is null)
            return;

        var service = App.Current.Services.GetService(value.MenuType);
        if (service is null)
            return;

        CurrentPage = (ViewModelBase)service;
    }

    /// <summary>
    /// 主题切换
    /// </summary>
    [RelayCommand]
    private void ToggleCheckedChanged()
    {
        var app = Application.Current;
        if (app is not null)
        {
            var theme = app.ActualThemeVariant;
            app.RequestedThemeVariant = theme == ThemeVariant.Dark ? ThemeVariant.Light : ThemeVariant.Dark;
        }
    }
}